package com.ruoyi.tzzl.service.imp;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.tzzl.domain.WzMatchmakerApply;
import com.ruoyi.tzzl.domain.WzProgramUser;
import com.ruoyi.tzzl.mapper.WzMatchmakerApplyMapper;
import com.ruoyi.tzzl.service.IWzMatchmakerApplyService;
import com.ruoyi.tzzl.service.IWzProgramUserService;
import com.sun.org.apache.bcel.internal.generic.NEW;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * 红娘申请Service业务层处理
 * @author by caoyong
 * @date 2022/11/9
 **/
@Service
public class WzMatchmakerApplyServiceImpl extends ServiceImpl<WzMatchmakerApplyMapper, WzMatchmakerApply> implements IWzMatchmakerApplyService {

    @Autowired
    IWzProgramUserService userService;


    @Override
    public List<WzMatchmakerApply> selectWzMatchmakerApplyList(WzMatchmakerApply wzMatchmakerApply) {
        LambdaQueryWrapper<WzMatchmakerApply> lqw =new LambdaQueryWrapper<>();
         lqw.eq(WzMatchmakerApply::getType,"0");
        //用户名称
        if (ObjectUtils.isNotEmpty(wzMatchmakerApply.getUserName())){
            lqw.eq(WzMatchmakerApply::getUserName,wzMatchmakerApply.getUserName());
        }
        //用户id
        if (ObjectUtils.isNotEmpty(wzMatchmakerApply.getUserId())){
            lqw.eq(WzMatchmakerApply::getUserId,wzMatchmakerApply.getUserId());
        }
        //申请时间
        if (ObjectUtils.isNotEmpty(wzMatchmakerApply.getCreateTime())){
            lqw.eq(WzMatchmakerApply::getCreateTime,wzMatchmakerApply.getCreateTime());
        }
        //状态审核
        if (ObjectUtils.isNotEmpty(wzMatchmakerApply.getStatus())){
            lqw.eq(WzMatchmakerApply::getStatus,wzMatchmakerApply.getStatus());
        }
        return this.list(lqw);
    }

    /**
     * 红娘申请审核
     *
     * @param wzMatchmakerApply
     */
    @Override
    public boolean matchmakerAudit(WzMatchmakerApply wzMatchmakerApply) {
        boolean ret =true;
       WzMatchmakerApply matchmakerApply = this.getById(wzMatchmakerApply.getId());
        WzProgramUser user =userService.getById(matchmakerApply.getUserId());
        if (wzMatchmakerApply.getStatus().equals("1")){ //审核通过
            user.setMatchmakerId(user.getId()); //红娘团队id
            user.setMatchmakerLevel("1"); //红娘等级
            user.setRole("3"); //用户角色
            userService.updateById(user);
            matchmakerApply.setStatus(wzMatchmakerApply.getStatus());
            ret =   this.updateById(matchmakerApply);
        }else if (wzMatchmakerApply.getStatus().equals("2")){ //审核拒绝
             matchmakerApply.setStatus("2");
            ret = this.updateById(matchmakerApply);
        }
        return ret;
    }

    /**
     * 查询红娘取消列表
     */
    @Override
    public List<WzMatchmakerApply> cancelMatchmakerList() {
        LambdaQueryWrapper<WzMatchmakerApply> lqw =new LambdaQueryWrapper<>();
        lqw.eq(WzMatchmakerApply::getType,"1");
        return this.list(lqw);
    }

    /**
     * 解除红娘资格
     *
     * @param wzMatchmakerApply
     */
    @Override
    public boolean removeQualification(WzMatchmakerApply wzMatchmakerApply) {
        //查询用户信息
       WzProgramUser user =userService.getById(wzMatchmakerApply.getUserId());
       user.setMatchmakerId(null); //团队红娘ID
       user.setUserLevel("1"); //用户等级
       user.setRole("1"); //用户角色
        user.setMatchmakerLevel(null); //红娘等级
       userService.updateById(user);
       LambdaQueryWrapper<WzMatchmakerApply> lqw =new LambdaQueryWrapper<>();
       lqw.eq(WzMatchmakerApply::getUserId,wzMatchmakerApply.getUserId());
       lqw.eq(WzMatchmakerApply::getStatus,"1");
      WzMatchmakerApply wzMatchmakerApply1 = this.getOne(lqw);
        BeanUtils.copyProperties(wzMatchmakerApply1,wzMatchmakerApply);
        wzMatchmakerApply.setId(null);
        wzMatchmakerApply.setType("1");
        wzMatchmakerApply.setCreateTime(new Date());
        return this.save(wzMatchmakerApply);
    }
}
